package com.neu.servlet.ww;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ChangeTimeServlet",urlPatterns = "/ChangeTimeServlet.do")
public class ChangeTimeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        Date date=new Date(session.getCreationTime());
        date.setMinutes(date.getMinutes()+20);
        Date cur=new Date();
        long time=(date.getTime()-cur.getTime())/1000;
        long m=time/60;
        long s=time%60;
        if (time == 0) {
            List<Map<String,Object>> carList=null;
            session.setAttribute("carList",carList);
        }
        Map<String,Object> dates=new HashMap<>();
        dates.put("mm",m);
        dates.put("ss",s);
        String json=JSONObject.toJSONString(dates);
        PrintWriter pw=response.getWriter();
        pw.write(json);
        pw.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
